// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   MysqlStore.java

package include.data_backup;

import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

// Referenced classes of package include.data_backup:
//			PropertiesReader

public class MysqlStore
{

	private Runtime rt;
	private PropertiesReader reader;
	private String fileName;
	private String userName;
	private String password;
	private String strURL1;

	public MysqlStore()
	{
		reader = new PropertiesReader();
		userName = reader.getProperty("username");
		password = reader.getProperty("password");
		String strClassName = getClass().getName();
		String strPackageName = "";
		if (getClass().getPackage() != null)
			strPackageName = getClass().getPackage().getName();
		String strClassFileName = "";
		if (!"".equals(strPackageName))
			strClassFileName = strClassName.substring(strPackageName.length() + 1, strClassName.length());
		else
			strClassFileName = strClassName;
		URL url = getClass().getResource((new StringBuilder(String.valueOf(strClassFileName))).append(".class").toString());
		String strURL = url.toString();
		strURL = strURL.substring(strURL.indexOf('/') + 1, strURL.lastIndexOf('/'));
		String strURL2 = strURL.substring(0, strURL.lastIndexOf("WEB-INF"));
		strURL1 = (new StringBuilder(String.valueOf(strURL2))).append("WEB-INF/").toString();
		try
		{
			rt = Runtime.getRuntime();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public boolean backup(String fileName, String database)
	{
		fileName = getBackupFileName(fileName);
		try
		{
			Properties properties = new Properties();
			InputStream inputstream = getClass().getClassLoader().getResourceAsStream("/conf/dbip.properties");
			properties.load(inputstream);
			if (inputstream != null)
				inputstream.close();
			String masterIp = properties.getProperty("IP");
			Process process = rt.exec((new StringBuilder("mysqldump  -x  -u")).append(userName).append(" -p").append(password).append(" -c --default-character-set=utf8  --databases ").append(database).append(" -h ").append(masterIp).append(" ").toString());
			String line = "";
			BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"));
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
			while ((line = bufferedReader.readLine()) != null) 
			{
				bufferedWriter.write(line.replace("text NOT NULL,", "text NOT NULL default '',"));
				bufferedWriter.newLine();
			}
			bufferedWriter.flush();
			bufferedWriter.close();
			process.waitFor();
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}
		catch (InterruptedException ie)
		{
			ie.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean recovery(String fileName)
	{
		String btfileName = writeHelperBat(fileName);
		try
		{
			Process process = null;
			String os = System.getProperties().getProperty("os.name");
			if (os.equals("Linux"))
			{
				CmdExec((new StringBuilder("chmod +x ")).append(btfileName).toString());
				process = rt.exec(btfileName);
			} else
			{
				process = rt.exec((new StringBuilder("cmd /E:OFF /c start ")).append(btfileName).toString());
			}
			String line = "";
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			while ((line = bufferedReader.readLine()) != null) 
				System.out.println(line);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private String getBackupFileName(String fileName)
	{
		String time = getTime();
		int index = fileName.lastIndexOf(".");
		if (index != -1)
			fileName = (new StringBuilder(String.valueOf(fileName.substring(0, index)))).append(fileName.substring(index, fileName.length())).toString();
		else
			fileName = fileName;
		fileName = (new StringBuilder(String.valueOf(strURL1))).append(fileName).toString();
		return fileName;
	}

	private String getRecoveryFileName(String fileName)
	{
		if (System.getProperties().getProperty("os.name").equals("Linux"))
			fileName = (new StringBuilder(String.valueOf(strURL1))).append(fileName).toString();
		else
			fileName = (new StringBuilder(String.valueOf(strURL1.substring(0, strURL1.length())))).append(fileName).toString();
		return fileName;
	}

	private String getTime()
	{
		Date now = new Date();
		SimpleDateFormat formater = new SimpleDateFormat("yy-MM-dd-hh-mm-ss");
		String time = formater.format(now);
		return time;
	}

	private String writeHelperBat(String fileName)
	{
		String btfile = getRecoveryFileName("do.bat");
		fileName = getRecoveryFileName(fileName);
		File file = new File(btfile);
		if (file.exists())
			file.delete();
		try
		{
			Properties properties = new Properties();
			InputStream inputstream = getClass().getClassLoader().getResourceAsStream("/conf/dbip.properties");
			properties.load(inputstream);
			if (inputstream != null)
				inputstream.close();
			String masterIp = properties.getProperty("IP");
			BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(btfile)));
			String content = (new StringBuilder("mysql -u")).append(userName).append(" -p").append(password).append(" -h ").append(masterIp).append(" <").append(fileName).toString();
			bufferedWriter.write(content);
			bufferedWriter.newLine();
			bufferedWriter.write("exit");
			bufferedWriter.flush();
			bufferedWriter.close();
		}
		catch (FileNotFoundException fe)
		{
			fe.printStackTrace();
		}
		catch (IOException ioe)
		{
			ioe.printStackTrace();
		}
		return btfile;
	}

	public void CmdExec(String cmdline)
	{
		try
		{
			Process p = null;
			if (p != null)
			{
				p.destroy();
				p = null;
			}
			p = Runtime.getRuntime().exec(cmdline);
			BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
			for (String line = input.readLine(); line != null; line = input.readLine());
			input.close();
			p.waitFor();
			int ret = p.exitValue();
		}
		catch (Exception err)
		{
			err.printStackTrace();
		}
	}

	public static void main(String args[])
	{
		MysqlStore store = new MysqlStore();
		store.recovery("backm.sql");
	}
}
